//
//  XS_MineInfoVC.swift
//  XueDao
//
//  Created by Mac on 2020/12/10.
//

import UIKit

class XS_MineInfoVC: XS_BaseVC {

    @IBOutlet weak var tableView: UITableView!
    
    var model : XS_MineInfoModel?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.backgroundColor = #colorLiteral(red: 0.968627451, green: 0.968627451, blue: 0.968627451, alpha: 1)
        title = "学员信息"
        model = XS_MineInfoModel.deserialize(from: XS_Singleton.shared.infoModel?.userBO.toJSON())
        tableView.contentInset = UIEdgeInsets.init(top: 0, left: 0, bottom: .bottomHeight, right: 0)
        tableView.register(UINib.init(nibName: "XS_SetListCell", bundle: Bundle.main), forCellReuseIdentifier: "XS_SetListCell")
        tableView.register(UINib.init(nibName: "XS_MineInfoIndustryCell", bundle: Bundle.main), forCellReuseIdentifier: "XS_MineInfoIndustryCell")
        tableView.delegate = self
        tableView.dataSource = self
    }
    
    func saveInfo() {
        XS_NetWork.requestJson("user/updateUser", parameters: model?.toJSON())
        .subscribe(onNext: { resp in //直接返回原始数据 resp.data
            if resp.success{
                self.tableView.reloadData()
            }else{
                self.view.makeToast(resp.msg)
            }
        }).disposed(by: disposeBag)
    }

}

extension XS_MineInfoVC : UITableViewDelegate,UITableViewDataSource{
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        case 0:
            return 1
        case 1:
            return 4
        case 2:
            return 1
        default:
            return 0
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        switch indexPath.section {
        case 0:
            let cell = tableView.dequeueReusableCell(withIdentifier: "XS_SetListCell", for: indexPath) as! XS_SetListCell
            cell.nextImageView.isHidden = false
            cell.titleLabel.text = "头像"
            cell.rightNextLabel.text = ""
            cell.rightLabel.text = ""
            cell.cellarSwitch.isHidden = true
            if model != nil {
                cell.rightImageView.sd_setImage(with: URL.init(string: model?.userImgSrc ?? ""), completed: nil)
            }
            return cell
            break
        case 1:
            let cell = tableView.dequeueReusableCell(withIdentifier: "XS_SetListCell", for: indexPath) as! XS_SetListCell
            switch indexPath.row {
            case 0:
                cell.nextImageView.isHidden = false
                cell.titleLabel.text = "昵称"
                if model != nil {
                    cell.rightNextLabel.text = model?.username
                }else{
                    cell.rightNextLabel.text = ""
                }
                cell.rightLabel.text = ""
                cell.cellarSwitch.isHidden = true
                cell.rightImageView.image = UIImage.init()
                break
            case 4:
                cell.nextImageView.isHidden = false
                cell.titleLabel.text = "一句话介绍自己"
                if model != nil {
                    cell.rightNextLabel.text = model?.introduce
                }else{
                    cell.rightNextLabel.text = ""
                }
                cell.rightLabel.text = ""
                cell.cellarSwitch.isHidden = true
                cell.rightImageView.image = UIImage.init()
                break
            case 1:
                cell.nextImageView.isHidden = false
                cell.titleLabel.text = "性别"
                cell.rightNextLabel.text = model?.sex ?? ""
                cell.rightLabel.text = ""
                cell.cellarSwitch.isHidden = true
                cell.rightImageView.image = UIImage.init()
                break
            case 5:
                cell.nextImageView.isHidden = false
                cell.titleLabel.text = "年龄"
                if model != nil {
                    cell.rightNextLabel.text = model?.birthday
                }else{
                    cell.rightNextLabel.text = ""
                }
                cell.rightLabel.text = ""
                cell.cellarSwitch.isHidden = true
                cell.rightImageView.image = UIImage.init()
                break
            case 2:
                cell.nextImageView.isHidden = false
                cell.titleLabel.text = "生日"
                if model != nil {
                    cell.rightNextLabel.text = model?.birthday.replacingOccurrences(of: "00:00:00", with: "")
                }else{
                    cell.rightNextLabel.text = ""
                }
                cell.rightLabel.text = ""
                cell.cellarSwitch.isHidden = true
                cell.rightImageView.image = UIImage.init()
                break
            case 3:
                cell.nextImageView.isHidden = false
                cell.titleLabel.text = "地区"
                if model != nil {
                    cell.rightNextLabel.text = model?.address
                }else{
                    cell.rightNextLabel.text = ""
                }
                cell.rightLabel.text = ""
                cell.cellarSwitch.isHidden = true
                cell.rightImageView.image = UIImage.init()
                break
            default:
                break
            }
            return cell
        case 2:
            let cell = tableView.dequeueReusableCell(withIdentifier: "XS_MineInfoIndustryCell", for: indexPath) as! XS_MineInfoIndustryCell
            cell.industryList = model?.industryList ?? []
            return cell
            break
        default:
            return UITableViewCell.init()
            break
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            return 88
        }else if indexPath.section == 1{
            return 50
        }
        return 102
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView.init(frame: CGRect.init(x: 0, y: 0, width: .screenWidth, height: 8))
        view.backgroundColor = UIColor.qmui_color(withHexString: "#F7F7F7")
        return view
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 1 || section == 2{
            return 8
        }
        return 0
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 0 {
            let picker = TZImagePickerController.init(maxImagesCount: 1, delegate: nil)
            picker?.allowTakeVideo = false
            picker?.allowPickingVideo = false
            picker?.allowCrop = true
            picker?.cropRect = CGRect.init(x: 0, y: (.screenHeight - .screenWidth ) / 2, width: .screenWidth, height: .screenWidth)
            picker?.didFinishPickingPhotosHandle = {(photos, assets, isSelectOriginPhoto) in
                if let data = photos?.first?.jpegData(compressionQuality: 0.1) {
                    XS_NetWork.xs_uploadFile(XS_NetWork.XS_UploadM(data: data)) { resp in
                        self.model?.userImgSrc = resp?.data ?? ""
                        self.saveInfo()
                    }
                }
            }
            picker?.modalPresentationStyle = .fullScreen
            present(picker!, animated: true, completion: nil)
        }else if (indexPath.section == 1){
            switch indexPath.row {
            case 0:
                let alert = UIAlertController.init(title: "昵称", message: "请输入昵称", preferredStyle: .alert)
                alert.addTextField { (textField) in
                    textField.text = self.model?.username ?? ""
                }
                alert.addAction(UIAlertAction.init(title: "取消", style: .cancel, handler: nil))
                alert.addAction(UIAlertAction.init(title: "确定", style: .default, handler: { (action) in
                    let textField = alert.textFields?.first
                    self.model?.username = textField?.text ?? ""
                    self.saveInfo()
                }))
                present(alert, animated: true, completion: nil)
                break
            case 4:
                /*
                let alert = UIAlertController.init(title: "个人介绍", message: "请输入个人介绍", preferredStyle: .alert)
                alert.addTextField { (textField) in
                    textField.text = self.model?.introduce ?? ""
                }
                alert.addAction(UIAlertAction.init(title: "取消", style: .cancel, handler: nil))
                alert.addAction(UIAlertAction.init(title: "确定", style: .default, handler: { (action) in
                    let textField = alert.textFields?.first
                    self.model?.introduce = textField?.text ?? ""
                    self.saveInfo()
                }))
                present(alert, animated: true, completion: nil)
                */
                break
            case 1:
                let alert = UIAlertController.init(title: "性别", message: "请选择性别", preferredStyle: .actionSheet)
                alert.addAction(UIAlertAction.init(title: "男", style: .default, handler: { (action) in
                    self.model?.sex = "男"
                    self.saveInfo()
                }))
                alert.addAction(UIAlertAction.init(title: "女", style: .default, handler: { (action) in
                    self.model?.sex = "女"
                    self.saveInfo()
                }))
                alert.addAction(UIAlertAction.init(title: "取消", style: .cancel, handler: nil))
                present(alert, animated: true, completion: nil)
                break
            case 5:
                BRDatePickerView.showDatePicker(with: .YMD, title: "请选择出生日期", selectValue: nil, minDate: nil, maxDate: Date.init(), isAutoSelect: false) { (date, stringDate) in
                    self.model?.birthday = stringDate ?? ""
                    self.saveInfo()
                }
                break
            case 2:
                BRDatePickerView.showDatePicker(with: .YMD, title: "请选择出生日期", selectValue: nil, minDate: nil, maxDate: Date.init(), isAutoSelect: false) { (date, stringDate) in
                    self.model?.birthday = stringDate ?? ""
                    self.saveInfo()
                }
                break
            case 3:
                BRAddressPickerView.showAddressPicker(withSelectIndexs: nil) { (pronvince, city, area) in
                    self.model?.address = (pronvince?.name ?? "") + (city?.name ?? "") + (area?.name ?? "")
                    self.saveInfo()
                }
                break
            default:
                break
            }
        }
    }
    
}
